﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SPPD.model;
using SPPD.controller;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace SPPD
{
	/// <summary>
	/// Interaction logic for pengajuan_spj.xaml
	/// </summary>
	public partial class pengajuan_spj : Window
	{
        public List<sppd> list_sppd = new List<sppd>();
        public int select;
        public sppd edit = new sppd();
        public pegawai pegawaiLogin = new pegawai();
        public pengajuan_spj(pegawai pgawai)
		{
            pegawaiLogin = pgawai;
			this.InitializeComponent();
            controller_sppd dummy = new controller_sppd();

            list_sppd = dummy.load();
            for (int i = 0; i < list_sppd.Count; i++)
            {
                if (list_sppd[i].status > 1)
                {
                    list_sppd.RemoveAt(i);
                }
            }
            data.ItemsSource = list_sppd;
			
			// Insert code required on object creation below this point.
		}

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window3 aju = new Window3(pegawaiLogin);
            this.Hide(); aju.Show();
        }

        public void a()
        {
            ComboBox a = new ComboBox();
            controller_divisi con = new controller_divisi();
            List<divisi> div = con.load();
            for (int i = 0; i < div.Count; i++)
            {
                a.Items.Add(div[i].nama_divisi);
            }
            pegawai peg = new pegawai();
            peg.divisi = div[a.SelectedIndex];
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            pegawai dummy_pegawai = new pegawai();
            controller_sppd cons = new controller_sppd();
            nhibernate nhi = new nhibernate();
            var factory = nhi.CreateSessionFactory();
            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    int idx = new int();
                    var categories = session.Query<pegawai>()
                           .OrderBy(c => c.nama)
                           .ToList();

                    for (int i = 0; i < categories.Count; i++)
                    {
                        if (categories[i].nama == textBox1.Text)
                        {
                            dummy_pegawai = categories[i];
                            idx = i;
                            MessageBox.Show(idx.ToString());
                            break;
                        }
                    }
                    var dr = session.Get<pegawai>(dummy_pegawai.id_pegawai);
                    MessageBox.Show(dr.id_pegawai);
                    var dummy = new sppd
                    {
                        ketua = dr,
                        tanggal_pengajuan = textBox3.Text,
                        tanggal_perjalanan = textBox4.Text,
                        tujuan = textBox5.Text,
                        kepentingan = textBox6.Text,
                        status = 1,
                        nama_file_sppd = list_sppd[select].nama_file_sppd,
                        nama_file_spj = textBox9.Text,
                        id_sppd = cons.autogenerate()
                        ,
                        id_spj = cons.autogenerateSPJ()

                    };
                    session.Save(dummy);
                    transaction.Commit();
                }
            }


            crud<sppd> n = new crud<sppd>();
            //MessageBox.Show(dummy.id_sppd);
            //n.insert(dummy);
            MessageBox.Show("proposal sudah diupload");
        }

        private void data_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            select = data.SelectedIndex;
            edit = list_sppd[select];           
        }

        private void button3_Click_1(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".doc"; // Default file extension
            dlg.Filter = "Text documents (.doc)|*.doc"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                textBox9.Text = filename;
            }
        }



        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".doc"; // Default file extension
            dlg.Filter = "Text documents (.doc)|*.doc"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                textBox8.Text = filename;
            }
        }
	}
}